/*
 * (C) Copyright 2006-2010 Nuxeo SAS (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * Contributors:
 *     bstefanescu
 */
package org.nuxeo.gwt.habyt.app.client;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
 * 
 */
public abstract class Action<T> {

    protected String type;

    protected T data;

    protected String confirm;

    protected String message;

    protected String target;

    protected String outcome;

    public Action(String type) {
        this(type, null, null);
    }

    public Action(String type, T data) {
        this(type, data, null);
    }

    public Action(String type, T data, String target) {
        this.type = type;
        this.data = data;
        this.target = target;
    }

    /**
     * Initialize the action data from a string definition
     * 
     * @param expr
     */
    public abstract void loadData(String expr);

    public String getType() {
        return type;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public void setTarget(String target) {
        this.target = target;
    }

    public String getTarget() {
        return target;
    }

    public void setOutcome(String outcome) {
        this.outcome = outcome;
    }

    public String getOutcome() {
        return outcome;
    }

    public void setConfirm(String confirm) {
        this.confirm = confirm;
    }

    public String getConfirm() {
        return confirm;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void execute(Widget trigger) {
        execute(new ActionContext(trigger));
    }

    public void execute(Widget trigger, Object content) {
        execute(new ActionContext(trigger, content));
    }

    public void execute(ActionContext ctx) {
        if (confirm != null) {
            if (!Window.confirm(confirm)) {
                return;
            }
        }
        doExecute(ctx);
    }

    protected abstract void doExecute(ActionContext ctx);

}
